package com.extension.company.view.main;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseFragment;
import com.extension.company.common.utils.Utils;
import com.extension.company.contract.main.MakeMoneyFragmentContract;
import com.extension.company.model.main.MakeMoneyFragmentMode;
import com.extension.company.presenter.home.MakeMoneyPrester;
import com.extension.company.presenter.main.MakeMoneyFragmentPresenter;
import com.extension.company.view.adapter.MakeMoneyLeftAdapter;
import com.extension.company.view.adapter.MakeMoneyRightAdapter;

/**
 * fragment  赚钱
 */
public class FragmentMakeMoney extends BaseFragment<MakeMoneyFragmentContract.View, MakeMoneyFragmentPresenter> implements MakeMoneyFragmentContract.View {

	private Activity activity;
	private LinearLayout m_top;
	private ListView mListView_Left;
	private RecyclerView mListView_Right;

	private MakeMoneyLeftAdapter leftAdapter;
	private MakeMoneyRightAdapter rightAdapter;
	private Context context;

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.activity = (Activity) context;
	}

	@Override
	public MakeMoneyFragmentContract.View createView() {
		return this;
	}

	@Override
	public MakeMoneyFragmentPresenter createPresenter() {
		return new MakeMoneyFragmentPresenter();
	}

	@Override
	public void init() {

		initView();
		initData();
		initListener();
	}

	@Override
	public int getMainLayout() {
		return R.layout.fragment_makemoney;
	}

	private void initView() {
		m_top = getViewById(R.id.m_top);
		//如果4.4及以上可以让状态栏透明
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
				&& Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
			layoutParams.height = Utils.getStatusBarHeight(activity);
			m_top.setLayoutParams(layoutParams);
			m_top.setBackgroundColor(activity.getResources().getColor(R.color.black2));
		} else {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
			layoutParams.height = Utils.getStatusBarHeight(activity);
			m_top.setLayoutParams(layoutParams);
			m_top.setBackgroundColor(activity.getResources().getColor(R.color.white));
		}
		mListView_Left = getViewById(R.id.mListView_Left);
		mListView_Right = getViewById(R.id.mListView_Right);
	}

	private void initData() {
//		leftAdapter = new MakeMoneyLeftAdapter(activity);
//		mListView_Left.setAdapter(leftAdapter);

//		rightAdapter = new MakeMoneyRightAdapter(activity);
		mListView_Right.setLayoutManager(new GridLayoutManager(context, 2));

		mListView_Right.setAdapter(rightAdapter);
	}

	private void initListener() {
		mListView_Right_OnItemClickListener();
		setmListView_Left_OnItemClickListener();
	}

	private void mListView_Right_OnItemClickListener() {
//		mListView_Right.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//				SellerPage.startActivity(activity);
//			}
//		});
	}

	private void setmListView_Left_OnItemClickListener() {
		mListView_Left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				changeLeftPosition(position);

				leftAdapter.setIndex(position);
				leftAdapter.notifyDataSetChanged();

				mListView_Left.smoothScrollToPositionFromTop(position, mListView_Left.getHeight() / 2 - view.getHeight() / 2);
			}
		});
	}


	private int scrollViewMiddle = 0;
	private int scrollViewWidth = 0;


	private void changeLeftPosition(int position) {
		int x = 0;
		if (position <= ((mListView_Left.getLastVisiblePosition() + mListView_Left.getFirstVisiblePosition()) / 2)) {
			x = position - getScrollViewMiddle();
		} else {
			x = position + getScrollViewMiddle();
		}
		mListView_Left.smoothScrollToPosition(x);

	}

	private int getScrollViewMiddle() {

		scrollViewMiddle = getScrollViewHeight() / 2;
		return scrollViewMiddle;
	}

	private int getScrollViewHeight() {
		scrollViewWidth = mListView_Left.getLastVisiblePosition();
		Log.d("zwk", "最后 " + scrollViewWidth);
		Log.d("zwk", (mListView_Left.getLastVisiblePosition() - mListView_Left.getFirstVisiblePosition()) + "一屏 多少个");
		return scrollViewWidth;
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}
}
